//
//  HeperTools.m
//  TimeDreamFM
//
//  Created by dllo on 15/12/7.
//  Copyright © 2015年 TimeInDream. All rights reserved.
//

#import "HeperTools.h"

static HeperTools *tools;
@implementation HeperTools

+(HeperTools *)shareHeperTools
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tools = [[HeperTools alloc]init];
    });
    return tools;
}

#pragma mark 时间戳转时间
/**
 *  自定义转换格式
 *
 *  @param timeSecond 时间戳
 *  @param format     格式
 *
 *  @return 
 */
+(NSString *)shareDateByTimeSecond:(id)timeSecond WithFormat:(NSString *)format
{
    //时间戳转标准时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    //设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSString *date = [NSString stringWithFormat:@"%@",[formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:[timeSecond longLongValue]]]];
    return date;
}

/**
 *  转换格式为2012.12.2  12:00
 *
 *  @param timeSecond 时间戳
 *
 *  @return
 */
+(NSString *)shareStandardDateByTimeSecond:(id)timeSecond
{
    //时间戳转标准时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    //设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSString *date = [NSString stringWithFormat:@"%@",[formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:[timeSecond longLongValue]]]];
    return date;
}

/**
 *  转换格式为12.2  12:00
 *
 *  @param timeSecond 时间戳
 *
 *  @return
 */
+(NSString *)shareMouthWithTimeByTimeSecond:(id)timeSecond
{
    //时间戳转标准时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM.dd hh:mm"];
    //设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSString *date = [NSString stringWithFormat:@"%@",[formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:[timeSecond longLongValue]]]];
    return date;
}



+(NSString *)shareGetUTCFormateDate:(id)timeSecond
{
    //时间戳转标准时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    //设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSString *date = [NSString stringWithFormat:@"%@",[formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:[timeSecond longLongValue]]]];
    NSDate *newsDateFormatted = [formatter dateFromString:date];
    //    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    //    [dateFormatter setTimeZone:timeZone];
    
    NSDate* current_date = [[NSDate alloc] init];
    
    NSTimeInterval time=[current_date timeIntervalSinceDate:newsDateFormatted];//间隔的秒数
    long year = ((long)time)/(3600*24*30*12);
    long month=((long)time)/(3600*24*30);
    long days=((long)time)/(3600*24);
    long hours=((long)time)%(3600*24)/3600;
    long minute=((long)time)%(3600*24)/60;
    //NSLog(@"time=%.2f",(double)time);
    
    NSString *dateContent;
    if (year != 0) {
        dateContent = [NSString stringWithFormat:@"%ld%@",year,@"年前"];
    }else if(month!=0){
        
        dateContent = [NSString stringWithFormat:@"%ld%@",month,@"个月前"];
        
    }else if(days!=0){
        
        dateContent = [NSString stringWithFormat:@"%ld%@",days,@"天前"];
    }else if(hours!=0){
        
        dateContent = [NSString stringWithFormat:@"%ld%@",hours,@"小时前"];
    }else{
        
        dateContent = [NSString stringWithFormat:@"%ld%@",minute,@"分钟前"];
    }
    return dateContent;
}

#pragma mark 获取文字所需要的尺寸
+(CGSize)shareSizeTextString:(NSString *)string WithFont:(UIFont *)font
             WithCGSize:(CGSize)size

{
//    if (size.width > fit_xOrWidth(375)) {
//        size.width = fit_xOrWidth(375);
//    }
    CGRect rect = [string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil] context:nil];
    
    return rect.size;
}


+ (NSString *)SharePlayCount:(long)count
{
    NSString *playCount = @"0";
    if (count >100000000) {
        playCount = [NSString stringWithFormat:@"%.2f亿",count/100000000.000];
    }else
        if (count > 10000) {
            playCount = [NSString stringWithFormat:@"%.1f万",count/10000.000];
        }else
        {
            playCount = [NSString stringWithFormat:@"%ld",count];
        }
    return playCount;
}



#pragma mark 清理缓存

#warning 清空
+(NSString *)cleanCaches
{
    //获取完整路径
    NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
    path = [path stringByAppendingPathComponent:@"Caches"];
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        NSArray *fileNameAarray = [manager subpathsAtPath:path];
        for (NSString *fileName in fileNameAarray) {
            //拼接绝对路径
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            
            //通过文件管理者删除文件
            [manager removeItemAtPath:absolutePath error:nil];
        }
    }
    return [NSString stringWithFormat:@"%.2fM",[HeperTools folderSizeAtPath:path]];
}

#warning 计算单个文件大小的方法
+(long long)fileSizeAtPath:(NSString *)path
{
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        return [[manager attributesOfItemAtPath:path error:nil]fileSize];
    }
    return 0;
}
#warning  计算一个文件夹的大小
+(float)folderSizeAtPath:(NSString *)path
{
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:path]) {
        return 0;
    }
    //根据路径获取文件夹里面元素集合
    //获取集合类型的枚举类型
    NSEnumerator *enumeratot = [[manager subpathsAtPath:path] objectEnumerator];
    //每次遍历得到的文件名
    //文件夹的大小
    NSString *fileName = [NSString string];
    float folderSize = 0;
    while ((fileName = [enumeratot nextObject])!= nil) {
        NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
        folderSize += [HeperTools fileSizeAtPath:absolutePath];
    }
    return folderSize/(1024*1024.0);
}

//关键字高亮
+(NSMutableAttributedString *)keyHightColorForStr:(NSString *)string keyStr:(NSString *)key 
{
    NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc]initWithString:string];
    NSRange range = [string rangeOfString:key];
    [attriString setAttributes:[NSMutableDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],NSForegroundColorAttributeName,[UIFont systemFontOfSize:16 weight:1],NSFontAttributeName, nil] range:range];
    
    return attriString;
}

+ (NSMutableAttributedString *)keyHightColorForStr:(NSString *)string keyStr:(NSString *)key WithColor:(UIColor *)color WithFont:(UIFont *)font
{
    NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc]initWithString:string];
    NSRange range = [string rangeOfString:key];
    [attriString setAttributes:[NSMutableDictionary dictionaryWithObjectsAndKeys:color,NSForegroundColorAttributeName,font,NSFontAttributeName, nil] range:range];
    
    return attriString;
}

+ (NSString *)shareConvertTime:(CGFloat)second
{
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:second];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    if (second/3600 >= 1) {
        [formatter setDateFormat:@"HH:mm:ss"];
    } else {
        [formatter setDateFormat:@"mm:ss"];
    }
    NSString *showtimeNew = [formatter stringFromDate:d];
    return showtimeNew;
}
@end
